<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%@include file="../common.jsp"%>

</head>
<body>
<script type="text/javascript">
	$(function() {
		$("#sysRoleList").datagrid({
			url : "sysRole/list",
			fitColumns : true,
			pagination : true,
			rownumbers : true,
			toolbar : "#toolbar"
		});
	})

	function setCondition() {
		var roleids = $("#roles").combobox("getValues");
		console.log(roleids);
		var postData = {};

		$("#sysRoleList").datagrid("reload", postData);
	}

	function resetCondition() {
		$("#conditionForm").form("clear");
	}

	function batchDelete() {
		var rows = $("#sysRoleList").datagrid("getSelections");
		if (rows.length == 0) {
			$.messager.alert("提示", "请选择要删除的数据", "info");
			return;
		}
		$.messager.confirm("提示", "确定删除吗？", function(r) {
			if (r) {
				var postData = "";
				$.each(rows, function(i) {
					postData += "ids=" + this.id;
					if (i < rows.length - 1) {
						postData += "&";
					}
				});

				$.post("sysRole/batchDelete", postData, function(data) {
					$("#sysRoleList").datagrid("reload");
				});
			}
		});
	}

	function addRole() {
		var d = $("<div></div>").appendTo("body");

		d.dialog({
			title : "添加角色",
			iconCls : "icon-add",
			width : 500,
			height : 300,
			modal : true,
			onClose : function() {
				$(this).dialog("destroy");
			},
			href : "sysRole/form",
			buttons : [ {
				text : "确定",
				iconCls : "icon-ok",
				handler : function() {
					$("#sysRoleForm").form("submit", {
						url : "sysRole/add",
						success : function(data) {
							d.dialog("close");
							$("#sysRoleList").datagrid("reload");
						}
					});
				}
			}, {
				text : "取消",
				iconCls : "icon-cancel",
				handler : function() {
					d.dialog("close");
				}
			} ]

		});
	}

	function edit() {
		var row = $("#sysRoleList").datagrid("getSelected");
		if (row == null) {
			return;
		}
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title : "修改角色",
			iconCls : "icon-update",
			width : 500,
			height : 300,
			modal : true,
			href : "sysRole/form",
			onClose : function() {
				$(this).dialog("destroy");
			},
			onLoad : function() {
				//当异步加载的页面回来以后
				$.post("sysRole/view/" + row.id, function(data) {
					$("#sysRoleForm").form("load", data);
				});
			},
			buttons : [ {
				text : "确定",
				iconCls : "icon-ok",
				handler : function() {
					$("#sysRoleForm").form("submit", {
						url : "sysRole/edit",
						success : function(data) {
							d.dialog("close");
							$("#sysRoleList").datagrid("reload");
						}
					});
				}
			}, {
				text : "取消",
				iconCls : "icon-cancel",
				handler : function() {
					d.dialog("close");
				}
			} ]

		});
	}
	
	function assignPermission(){
		var row = $("#sysRoleList").datagrid("getSelected");
		if(row == null){
			return;
		}
		
		$("#sysRoleList").datagrid("clearSelections");
		$("#sysRoleList").datagrid("selectRecord",row.id);
		
		var d = $("<div></div>").appendTo("body");
		d.dialog({
			title:"分配权限",
			width:250,
			height:450,
			modal:true,
			href:"sysRole/assgin?rid="+row.id,
			onClose:function(){$(this).dialog("destroy");},
			buttons:[{
				text:"确定",
				iconCls:"icon-ok",
				handler:function(){
					//获取选中的树形节点，获取id
					var checked = $("#assignPermissions").tree("getChecked","checked");
					var half =  $("#assignPermissions").tree("getChecked","indeterminate");
					$.merge(checked,half);
					//发送异步请求
					var postData = "rid="+row.id+"&";
					$.each(checked,function(){
						postData += "perIds="+this.id + "&";
					});
					$.post("sysRole/assignRights",postData,function(){
						d.dialog("close");
						$.messager.show({
							title:'系统提示',
							msg:'权限分配成功.',
							timeout:3000,
							showType:'slide'
						});
					});
				}
			},{
				text:"取消",
				iconCls:"icon-cancel",
				handler:function(){
					d.dialog("close");
				}
			}]
		});
	}
</script>
	<div class="easyui-panel" title="设置查询条件"
		data-options="collapsible:true"
		style="margin-bottom: 15px; padding: 10px;">
		<form action="" id="conditionForm">
			名称：<input type="text" name="" id="username" /> <a id="btn"
				href="javascript:void(0)" onclick="setCondition()"
				class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
			<a id="btn" href="javascript:void(0)" onclick="resetCondition()"
				class="easyui-linkbutton" data-options="iconCls:'icon-redo'">重置条件</a>
		</form>
	</div>
	<table id="sysRoleList">
		<thead>
			<tr>
				<th data-options="checkbox:true,field:'hello'"></th>
				<th data-options="field:'id',width:50,sortable:true">编号</th>
				<th data-options="field:'name',width:100,sortable:true">名称</th>
				<th data-options="field:'available',width:100,sortable:true">排序</th>
			</tr>
		</thead>
	</table>
	<div id="toolbar">
		<a id="btn" href="javascript:void(0)" onclick="addRole()"
			class="easyui-linkbutton" data-options="iconCls:'icon-add'">添加</a> <a
			id="btn" href="javascript:void(0)" class="easyui-linkbutton"
			data-options="iconCls:'icon-edit'" onclick="edit()">编辑</a>
		 <a
			id="btn" href="javascript:void(0)" onclick="batchDelete()"
			class="easyui-linkbutton" data-options="iconCls:'icon-remove'">删除</a>
		<a id="btn" href="javascript:void(0)" class="easyui-linkbutton"
			data-options="iconCls:'icon-undo'">导出</a> 
		<a id="btn"
			href="javascript:void(0)" class="easyui-linkbutton"
			data-options="iconCls:'icon-more'" onclick="assignPermission()">权限管理</a>
	</div>
</body>
</html>